<template>
	<view>
		<navbar title="修改信息">
		</navbar>
		<u-loading-page fontSize="16" bgColor="#fff" iconSize="30" loadingText="数据加载中" :loading="!success"
			loading-mode="semicircle"></u-loading-page>
		<view v-show="success">
			<view style="margin: 10px;padding: 10px;background-color: white;border-radius: 5px;font-size: 14px;">
				<view style="display: flex;align-items: center;justify-content: space-between;">
					<p>IP流量：</p>
					<p>{{info.ip}}</p>
				</view>
				<view style="display: flex;align-items: center;justify-content: space-between;">
					<p>下载人数：</p>
					<p>{{info.down}}</p>
				</view>
				<view style="display: flex;align-items: center;justify-content: space-between;">
					<p>文件大小：</p>
					<p>{{info.size>10486?(info.size/(1024*1024)).toFixed(2)+'MB':(info.size/(1024)).toFixed(2)+'KB'}}
					</p>
				</view>
				<view style="display: flex;align-items: center;justify-content: space-between;">
					<p>上传时间：</p>
					<p>{{info.time}}</p>
				</view>
			</view>
			<view style="margin: 10px;padding: 10px;background-color: white;border-radius: 5px;font-size: 14px;">
				<u--form labelPosition="top" :model="info">
					<u-form-item label="名称">
						<u--input v-model="info.title"></u--input>
					</u-form-item>
					<u-form-item label="包名" v-if="info.package!=null">
						<u--input v-model="info.package"></u--input>
					</u-form-item>
					<u-form-item label="版本">
						<u--input v-model="info.version"></u--input>
					</u-form-item>
					<u-form-item label="密码">
						<u-switch v-model="info.password_mode" @change="setswitch"></u-switch>
					</u-form-item>
					<u-form-item label="密码" v-if="info.password_mode">
						<u--input placeholder="请输入密码" v-model="info.password"></u--input>
					</u-form-item>
					<u-form-item label="介绍">
						<u--textarea maxlength='-1' v-model="info.content" count placeholder="请输入内容"></u--textarea>
					</u-form-item>
					<u-form-item>
						<u-button type="primary" @click="setinfo({
							content:info.content,
							password:info.password,
							version:info.version,
							package:info.package,
							title:info.title
						})" text="修改"></u-button>
					</u-form-item>
				</u--form>
			</view>
			<view style="margin: 10px;padding: 10px;background-color: white;border-radius: 5px;font-size: 14px;">
				<view style="display: flex;align-items: center;">
					<view style="flex: 1;display: flex;align-items: center;justify-content:flex-end;">
						<uni-file-picker :value="iconLists" limit="1" :auto-upload="false" :del-icon="false"
							disable-preview :imageStyles="imageStyles" file-mediatype="image"
							@select="iconupload">更新</uni-file-picker>
						<p>文件图标</p>
					</view>
				</view>
			</view>
			<view style="margin: 10px;padding: 10px;background-color: white;border-radius: 5px;font-size: 14px;">
				<view class="example-body">
					<uni-file-picker :value="imageLists" :auto-upload="false" :sizeType="sizetype" limit="9"
						title="展示图片" @select="imageupload" @delete="deleImage"></uni-file-picker>
				</view>
			</view>
			<view style="margin: 10px;padding: 10px;background-color: white;border-radius: 5px;font-size: 14px;">
				<view @click="updateapk" style="display: flex;justify-content:space-between;">
					<view style="display: flex;align-items: center;">
						<u-icon name="file-text-fill" size="30"></u-icon>
						<p>{{btn}}</p>
					</view>
					<view style="display: flex;align-items: center;">
						<u-icon name="arrow-right" size="25"></u-icon>
					</view>
				</view>
				<u-divider style="margin: 10px 0;"></u-divider>
				<view @click="()=>{goPage('template','?id='+_id);hide=true}"
					style="display: flex;justify-content:space-between;">
					<view style="display: flex;align-items: center;">
						<u-icon name="bookmark-fill" size="30"></u-icon>
						<p>更新模板</p>
					</view>
					<view style="display: flex;align-items: center;">
						<p>{{openup}}</p>
						<u-icon name="arrow-right" size="25"></u-icon>
					</view>
				</view>
			</view>
			<p style="height: 20px;"></p>
		</view>
		<UploadProgress ref="UploadProgress" :loading="progressLoading" :show="progressShow" @close="closeProgress()"
			:list="progresslist">
		</UploadProgress>
	</view>
</template>

<script>
	const db = uniCloud.database();
	import ase from '@/common/ar-aes/ar-aes.js'
	import store from '@/store/index.js';
	export default {
		data() {
			return {
				iconLists: [],
				imageLists: [],
				progressLoading: false,
				success: false,
				hide: false,
				imageStyles: {
					width: 64,
					height: 64,
					border: {
						radius: '50%'
					}
				},
				extension: [],
				info: {
					openup: null,
					ip: 0,
					down: null,
					size: null,
					title: null,
					package: null,
					version: null,
					content: null,
					time: null
				},
				btn: "更新文件",
				capture: ['album'],
				sizetype: ['compressed'],
				openup: null,
				_id: null,
				progresslist: [],
				progressShow: false
			};
		},
		onUnload() {
			uni.$emit("updateFile", this._id)
		},
		onShow() {
			if (this.hide) {
				uni.showLoading({
					title: '正在更新中'
				});
				this.getinfo(this._id)
			}
		},
		watch: {
			progresslist(val) {
				if (val.length > 0) {
					this.progressShow = true
				}
			}
		},
		methods: {
			closeProgress() {
				this.progressShow = false
				this.progresslist = []
			},
			deleImage(e) {
				this.info.image.splice(this.info.image.indexOf(e.tempFilePath), 1)
				this.setinfo({
					image: this.info.image
				})
			},
			getopenup(openup) {
				db.collection("template").where({
					openup: openup
				}).get({
					getOne: true
				}).then(res => {
					try {
						this.openup = res.result.data.title
					} catch (err) {
						this.openup = "默认"
					}
				});
			},
			getinfo(id) {
				db.collection("item").where({
					_id: id
				}).get({
					getOne: true
				}).then(res => {
					this.info = res.result.data
					this.iconLists = [{
						url: this.info.icon
					}]
					this.imageLists=this.info.image.map(item => {
						return {
							url: item
						}
					})
					this.getopenup(this.info.openup)
					this.info.time = uni.$u.timeFormat(this.info.time, 'yyyy-mm-dd hh:MM');
					uni.hideLoading()
					this.success = true
				})
			},
			setswitch(e) {
				this.info.password_mode = e
				this.setinfo({
					password_mode: e
				})
			},
			setinfo(data) {
				db.collection("item").where({
					_id: this._id
				}).update(data).then(res => {
					this.message("修改成功")
				})
			},
			async upload(file) {
				return new Promise((resolve) => {
					this.uploadFile(file, function(res) {
						resolve(res)
					})
				})
			},
			updateapk() {
				if (store.state.userInfo.vip < new Date().getTime()) {
					this.message("该功能为会员专属", "error")
					this.goPage("pay")
					return
				}
				if (this.btn != "更新文件") {
					return null
				}
				var that = this
				const plugin = uni.requireNativePlugin('GuoWei-SelectFileModule')
				plugin.chooseFile({
					count: 1,
					extension: this.extension
				}, res => {
					if (res.code == 1) {
						return
					}
					if (res.files[0].size >= 1024 * 1024 * 100) {
						this.message("文件不得超过100MB", "error")
						return
					}
					that.progressLoading = true
					that.progressShow = true
					setTimeout(() => {
						that.uploadFileToLanzou(res.files[0].path, (upLanRes) => {
							let downInfo = JSON.parse(upLanRes.data)
							let pwdInfo = JSON.parse(upLanRes.password)
							let lanzouUrl =
								`${downInfo.text[0].is_newd}/${downInfo.text[0].f_id}?pwd=${pwdInfo.info.pwd}`
							that.setinfo({
								url: lanzouUrl
							})
							that.progressLoading = false
							that.progressShow = false
							that.message("更新成功")
							that.btn = "更新文件";
						})
					}, 500)
				})
			},
			iconupload(e) {
				if (store.state.userInfo.vip < new Date().getTime()) {
					this.message("该功能为会员专属", "error")
					this.goPage("pay")
					return
				}
				this.upload(e.tempFilePaths[0]).then(res => {
					this.setinfo({
						icon: res
					})
				})
			},
			async imageupload(e) {
				if (store.state.userInfo.vip < new Date().getTime()) {
					this.message("该功能为会员专属", "error")
					this.goPage("pay")
					return
				}
				uni.showLoading({
					title: '图片上传中'
				});
				Promise.all(e.tempFilePaths.map(item => {
					return this.upload(item)
				})).then(res => {
					this.setinfo({
						image: this.info.image.concat(res)
					})
				})
			}
		},
		onLoad(op) {
			this._id = op.id
			this.getinfo(op.id)
			if (op.type == "apk") {
				this.extension = ['apk', 'ipa']
			} else {
				this.extension = ['.zip', '.rar', '.jar', '.7z', '.exe']
			}
		}
	}
</script>